%main = sqBrackets(piece,main,pos)
%

%%

function main = sqBrackets(piece,main,pos)

%% Predefining
load .\Core\temp\symbol.mat

%For the charge and hydrogen checking sections
c = 0;
h = 0;

%% Start code

%% Isotope
%This forloop checks for an isotope number. This code can handle any length
%of isotope number. SMILES should however never contain a number of longer
%than three digits, since there are no (relatively) stable elements found
%of over 294 atomic units in mass.
for j = length(piece):-1:1
    if ~isnan(str2double(piece(1:j)))
        main(pos).isotope = str2double(piece(1:j));
        %The next line cuts away the isotope index number to leave only the
        %unanalysed part of 'piece'.
        piece = piece(j+1:length(piece));
        break
    end
end

%% Charge
%The following forloop check for charges of a single sign. If both signs
%are used in one ion, then the whole program is terminated because of
%violation of the SMILES rules.
%Charges can be present in two formats: The newer accepted format which
%consists of first the sign and then the number of elementary charge (for
%example +4, +3, -2 or simply + or - because the 1 is omitted). Or the
%older format, which actually has been abandoned by the original minds
%behind SMILES but is still used by a lot of people out of tradition, which
%just consists of the number of signs the charge is made up of (for example
%++++, +++, -- instead of respectively +4, +3 and -2 in the newer format,
%single charges are still + or -).
for j = 1:length(piece)
    %This switch checks for + and - signs.
    switch piece(j)
        case '+'
            if exist('firstc','var') ~= 1
                firstc = j;
            end
            if c >= 0
                c = c + 1;
            else
                error('Both negative and positive signs for one ion.')
            end
        case '-'
            if exist('firstc','var') ~= 1
                firstc = j;
            end
            if c <= 0
                c = c - 1;
            else
                error('Both negative and positive signs for one ion.')
            end
    end
    %This if-statement checks for a numeric charge multiplier.
    if ~isnan(str2double(piece(j))) && c~=0
        if exist('multc','var') == 0
            multc = str2double(piece(j));
        else
            multc = multc*10 + str2double(piece(j));
        end
    end
end
%Now if a charge is found it is calculated and stored in the variable that
%will be put into the main cell.
if c ~= 0
    if exist('multc','var') == 1
        C = multc*c;
    else
        C = c;
    end
    if firstc ~= 0
        %This cuts away the now unanalysed part of the original inputted
        %string 'piece'.
        piece = piece(1:firstc-1);
    end
end

%% Isomers
%Isomeric SMILES:
for j = length(piece):-1:1
    for k = j-1:-1:0
        reference = {'@','@@','@@@','@TH','@AL','@SP',...
            '@TB','@OH'};
        z = find(ismember(reference,piece(j-k:j)));
        if ~isempty(z) && ~exist('piece2')
            for l = length(piece):-1:j
                if ~isnan(str2double(piece(j+1:l)))
                    main(pos).isomernum = str2double(piece(j+1:l));
                    break
                end
            end
            main(pos).isomeric = reference{z};
            piece2 = piece(1:j-k-1);
            break
        end
    end
end
if exist('piece2')
    piece = piece2;
end

%% Explicit Hydrogens
%This checks for explicit hydrogens by first checking every bit of the
%inputted string for the capital letter H
for j = 1:length(piece)
    switch piece(j)
        case 'H'
            if exist('firsth','var') ~= 1
                %If this letter has been found, 'firsth' will carrying the
                %value of the first position is has been found
                firsth = j;
            end
            if h >= 0
                %For every letter H 'h' is increased by one
                h = h + 1;
            end
    end
    
    %This if-statement checks for a numeric hydrogen multiplier.
    if ~isnan(str2double(piece(j))) && h~=0
        if ~exist('multh','var')
            multh = str2double(piece(j));
        else
            multh = multh*10 + str2double(piece(j));
        end
    end
end

%If at least one explicit hydrogen has been found
if h >= 1
    if exist('multh','var') == 1
        %And a multiplier has been assigned, then the amount of found
        %hydrogen atoms 'H' is the product of the multiplier and 'h' which
        %should be 1.
        H = multh*h;
    else
        %With no muliplier 'H' simply takes over the value of 'h'.
        H = h;
    end
    if firsth ~= 0
        %This cuts away the now unanalysed part of the original inputted
        %string 'piece'.
        piece = piece(1:firsth-1);
    end
end

%If however there has only been found a single Hydrogen and after removal
%of the hydrogen an empty string 'piece' is left, then the hydrogen was not
%attached to another atom withing the brackets and was actually not
%explicit but simply a lone hydrogen atom. In order to enable the drawing
%of these 'piece' is restored to contain 'H' for the element check ahead
%and the variable 'H' is set to zero.
if isempty(piece) && H == 1
    H = 0;
    piece = 'H';
end

%% Element
%The getElement function is used to get the element from the leftover
%unanalysed part of 'piece' which should now consist of only an element
%indicator.
main = getElement(piece,main,pos);

%% Output creation
if exist('C','var') == 1
    main(pos).charge = C;
end
if exist('H','var') == 1
    main(pos).hydrogen = H;
else
    main(pos).hydrogen = 0;
end

%% End code
end